// pages/index/room/index.js
const util = require("../../../utils/util");
import Toast from "../../../vant/weapp/toast/toast";
const app = getApp()
// let times = util.formatTimeHour(new Date()).split(":")
// let hourList = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"]
// let fenList = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"]
// let hour = hourList.slice(times[0])
// let sencend = fenList.slice(times[1])
// let arr = {}
// let keyMap = {}
// let obj = {}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    vip:0,
    pricecouponList: '[]',
    cardcouponList: '[]',
    room_name: '',
    couponsum: '',
    couponshow: false,
    couponshowif: 1,
    room: [],
    index: 0, //房间
    cardList: [],
    date_start: "",
    time_start: "", //开始
    date_end: "",
    time_end: "",
    time: 0, //套餐时间
    package_id: 0,
    privateRoom: [],
    chooseIndex: -1,
    store_id: 0, //门店id
    is_user_show: true,
    is_user: 0,
    showUser: false,
    show: false,
    img: [],
    isList: true,
    swiperH: '400rpx',
    indexList: [],
    coupon: '',
    preList: [{
        url: "/image/card3.png",
        name: "本店充值",
        subtitle: "本店快速充值",
        link: "/pages/person/vipplas/index",
      }, {
        url: "/image/welfare.png",
        name: "新人福利",
        subtitle: "老板发放福利啦",
        link: "/pages/present/index",
      },

    ],
    // seat: [],
    // multiIndex: [0, 0],
    // multiArray: [{
    //     values: Object.keys(obj),
    //     className: 'column1',
    //   },
    //   {
    //     values: obj[hour[0]],
    //     className: 'column2',
    //     // defaultIndex: 2,
    //   },
    // ],
    // pick_show_start: false,
    // pick_hour_show_start: false,
    // formatter(type, value) {
    //   if (type === "year") {
    //     return `${value}年`;
    //   } else if (type === "month") {
    //     return `${value}月`;
    //   }
    //   return `${value}日`;
    // },
    // currentDate_start: new Date().getTime(),
    // minDate: new Date().getTime(),
  },
  // onChange(event) {
  //   const {
  //     picker,
  //     value,
  //     index
  //   } = event.detail;
  //   picker.setColumnValues(1, obj[value[0]]);
  // },
  onShareAppMessage() {
    const that = this
    console.log(that.data.store_id)
    console.log(wx.getStorageSync("uid"))
    return {
      title: '会员邀请新人下单送福利啦',
      desc: '新人下单送30余额',
      path: '/pages/index/index?room_id='+that.data.store_id+'&uid='+wx.getStorageSync("uid"), // 路径，传递参数到指定页面。
    }
  },
  onLoad(options) {
    const that = this
    console.log(options)
    // that.data.date_start = that.data.date_end = util.formatTimeDay(new Date())
    if (options.room_id) {
      that.setData({
        store_id: options.room_id
      })
      that.getRoom()
      that.getRoomindex(options.room_id)
      return
    }
    if (options.scene != undefined) {
      const scene = decodeURIComponent(options.scene)
      let arrPara = scene.split('&');
      let arrPara1 = []
      console.log(arrPara)
      for (let i = 0; i < arrPara.length; i++) {
        arrPara1.push(arrPara[i].split('='))
      }
      that.setData({
        store_id: arrPara1[1][1]
      })
      that.getRoom()
      that.getRoomindex(arrPara1[1][1])
      return
    }

  },
  hide_notice() {

    this.setData({
      couponshow: false,
      couponshowif: 0
    })
    //couponshowstore_id
    wx.setStorageSync('couponshow' + this.data.store_id, 1)

    console.log(this.data.store_id)
    wx.navigateTo({
      url: '/pages/present/index?room_id=' + this.data.store_id,
    });
  },
  fail() {
    Toast.fail("请更改时间或选择其他包厢")
  },
  // bindMultiPickerColumnChange(e) {
  //   const that = this

  //   let data = {
  //     multiArray: that.data.multiArray,
  //     multiIndex: that.data.multiIndex
  //   };
  //   let times = util.formatTimeHour(new Date()).split(":")
  //   let sencend = that.data.fenList.slice(times[1])
  //   if (e.detail.column == 0 && e.detail.value != 0) {
  //     data.multiIndex[1] = 0;
  //     data.multiArray[1] = that.data.fenList
  //     that.setData(data);
  //   } else if (e.detail.column == 0 && e.detail.value == 0) {
  //     data.multiIndex[1] = 0;
  //     data.multiArray[1] = sencend
  //     that.setData(data);
  //   }
  // },
  // bindMultiPickerChange(e) {
  //   const that = this
  //   console.log("bindMultiPickerChange", e.detail.value, that.data.multiIndex)
  //   that.setData({
  //     multiIndex: e.detail.value
  //   })
  //   that.time()
  // },
  onShow: function () {
    const that = this
    that.getUserOrderInfo()
  },
  // 跳转
  linkTo(e) {
    console.log(e, this.data.store_id);
    const url = e.currentTarget.dataset.link;
    console.log(`${url}?room_id=${this.data.store_id}`)
    wx.navigateTo({
      url: `${url}?room_id=${this.data.store_id}`,
    });
  },

  chooseTime(e) {

    const id = e.currentTarget.dataset.id;
    const type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: "/pages/person/pricecoupon/index?id=" + id + "&type=" + type,
    });
  },

  // 打电话
  call() {
    const that = this
    let mobile = that.data.room[that.data.index].mobile
    wx.makePhoneCall({
      phoneNumber: mobile
    })
  },
  // 导航
  duo_map() {
    const that = this;
    let data = that.data.room[that.data.index]
    wx.getLocation({
      type: "wgs84",
      success: function (res) {
        wx.openLocation({
          //​使用微信内置地图查看位置。
          latitude: Number(data.lat), //要去的纬度-地址
          longitude: Number(data.lng), //要去的经度-地址
          name: data.name,
          address: data.address,
          scale: 18,
        });
      },
      fail(e) {
        console.log(e);
        wx.setStorageSync("openSet", true);
        that.openSet();
      },
    });
  },
  //   登录
  login() {
    if (wx.getStorageSync('token') == "") {
      this.setData({
        is_user_show: false,
        is_user: 1
      });
    } else if (wx.getStorageSync('mobile') == "") {
      this.setData({
        is_user_show: false,
        is_user: 2
      });
    }
  },
  // 登录遮罩层
  close(e) {
    this.setData({
      is_user_show: e.detail,
      is_user: 0
    });
  },
  // 登陆后更新
  renew(e) {
    console.log(e)
    this.setData({
      showUser: true,
    });
    wx.setStorageSync('userInfo', e.detail)
    wx.setStorageSync('mobile', e.detail.mobile)
    console.log(e.detail)
  },
  // 门店列表
  getRoom() {
    const that = this

    app.http.post(app.api.room, {}, (res) => {
      that.setData({
        room: res.data
      })
      console.log(that.data.store_id)
      if (that.data.store_id == 0) {
        let store_id = that.data.room[that.data.index].room_id.toString()
        that.setData({
          store_id,
          imgUrls: that.data.room[that.data.index].image_image
        })
        // that.getPackage()
        that.getRoomindex(store_id)
      } else {
        let rooms = that.data.room;

        for (let i in rooms) {
         // console.log(rooms[i].room_id)
          if (rooms[i].room_id == that.data.store_id) {
            console.log(that.data.room, that.data.index, rooms[i])
            that.setData({
              index: i,
              imgUrls: rooms[i].image_image
            })
            // that.getPackage()
            // 找到就跳出循环
            return
          }
        }
      }
    })
  },
  // bindChangePage(e) {
  //   console.log(e)
  //   const that = this
  //   that.setData({
  //     time: e.detail.value.time,
  //     package_id: e.detail.value.id
  //   })
  //   that.time()
  //   that.onClose()
  //   that.toOrder(1)
  // },
  // 更改时间
  // time() {
  //   const that = this
  //   // let start_time = that.data.multiArray[0][that.data.multiIndex[0]] + ':' + that.data.multiArray[1][that.data.multiIndex[1]]
  //   let now_time = that.data.date_start + " " + that.data.time_start;
  //   let next_time = new Date(now_time.replace(/\-/g, "/")).setHours(
  //     new Date(now_time.replace(/\-/g, "/")).getHours() + that.data.time
  //   );

  //   let time_end = util.formatH(next_time);
  //   let date_end = util.formatDate(next_time);
  //   that.setData({
  //     time_end,
  //     date_end,
  //     // time_start: start_time,
  //   })
  //   that.getRoomindex()
  // },
  // 选择套餐
  // chooseTime(e) {
  //   // console.log(e.currentTarget.dataset.id, e.currentTarget.dataset.index)
  //   const that = this
  //   that.setData({
  //     chooseIndex: e.currentTarget.dataset.index,
  //     package_id: e.currentTarget.dataset.id,
  //     time: e.currentTarget.dataset.time,
  //   })
  //   that.time()
  //   // that.getRoomindex()
  // },

  // 套餐列表
  // getPackage() {
  //   const that = this
  //   let store_id = that.data.room[that.data.index].room_id.toString()
  //   app.http.post(app.api.Package, {
  //     store_id
  //   }, (res) => {
  //     // console.log(res)
  //     that.setData({
  //       cardList: res.data
  //     })
  //   })
  // },
  // 包厢列表
  getRoomindex(store) {
    const that = this
    if (!wx.getStorageSync('couponshow' + store)) {
      app.http.post(app.api.coupontc, {
        store_id: store
      }, (res) => {

        if (res.data == 0) {
          this.setData({
            vip:res.vip,
            couponshow: false,
          })
        } else {

          this.setData({
            vip:res.vip,
            room_name: res.room.room_name,
            couponsum: res.data,
            couponshow: true,
          })
        }
      })
    }
    app.http.post(app.api.addcoupon, {
      store_id: store
    }, (res) => {
      this.setData({
        pricecouponList: res.data ? res.data : [],
      })
    })

    app.http.post(app.api.cardcoupon, {
      store_id: store
    }, (res) => {
      this.setData({
        cardcouponList: res.data ? res.data : [],
      })
    })
    let add_time = that.data.date_start + " " + that.data.time_start;
    let store_id
    if (store) {
      store_id = store
    } else {
      store_id = that.data.room[that.data.index].room_id.toString()
    }
    app.http.post(app.api.Roomindex, {
      store_id,
      package_id: that.data.package_id,
      add_time
    }, (res) => {
      if (res.data == "" || res.code === 401) {
        that.setData({
          privateRoom: res.data,
          isList: true
        })
      } else {
        that.setData({
          privateRoom: res.data,

          isList: false
        })
      }
    })
    // 



  },
  // 改图片
  changeImg(e) {
    const that = this;

    let img = e.currentTarget.dataset.img
    console.log(img)
    let imgs = JSON.stringify(img)
    if (img.length <= 0) {
      return
    }
    wx.navigateTo({
      url: '/pages/index/swiper/index?img=' + imgs,
    })
  },
  // 切换门店
  bindPickerChange(e) {
    const that = this
    that.setData({
      chooseIndex: -1,
      index: e.detail.value,
      store_id: that.data.room[e.detail.value].room_id,
      imgUrls: that.data.room[e.detail.value].image_image
    })
    that.getRoomindex(that.data.room[e.detail.value].room_id)
    // that.getPackage()
    that.getRoom()
  },
  // 首页开门
  getUserOrderInfo() {
    const that = this
    if (wx.getStorageSync('mobile') == "") {
      return
    }
    app.http.post(app.api.userOrderInfo, {}, (res) => {
      if (res.data == "") {
        return
      }
      if (res.data[0].order === 1) {
        that.setData({
          indexList: res.data[0]
        })
      } else {
        that.setData({
          indexList: {
            door_list: [{
              door_name: "开门"
            }],
          }
        })
      }
    })
  },
  // bindChange(e) {
  //   const that = this
  //   console.log(e)
  //   if (e.detail.value[0] < 10) {
  //     e.detail.value[0] = "0" + e.detail.value[0]
  //   }
  //   let time_start = e.detail.value[0] + ":" + e.detail.value[1]
  //   that.setData({
  //     pick_hour_show_start: false,
  //     time_start
  //   })
  //   that.time()
  // },
  //   日期选择器
  // bindDateChange: function (e) {
  //   const that = this
  //   if (util.formatTimeDay(new Date(e.detail)) != util.formatTimeDay(new Date())) {
  //     for (let i = 0; i < hourList.length; i++) {
  //       obj[i] = fenList
  //     }
  //     that.setData({
  //       multiArray: [{
  //           values: Object.keys(obj),
  //           className: 'column1',
  //         },
  //         {
  //           values: obj[hourList[0]],
  //           className: 'column2',
  //           // defaultIndex: 2,
  //         },
  //       ]
  //     })
  //   } else {
  //     for (let i = 1; i < hour.length; i++) {
  //       arr[0] = sencend
  //       arr[i] = fenList
  //       keyMap[0] = hour[0]
  //       keyMap[i] = hour[i]
  //     }
  //     obj = Object.keys(arr).reduce((newData, key) => {
  //       let newKey = keyMap[key]
  //       newData[newKey] = arr[key]
  //       return newData
  //     }, {})
  //   }
  //   that.setData({
  //     date_start: util.formatTimeDay(new Date(e.detail)),
  //     multiArray: [{
  //         values: Object.keys(obj),
  //         className: 'column1',
  //       },
  //       {
  //         values: obj[hour[0]],
  //         className: 'column2',
  //         // defaultIndex: 2,
  //       },
  //     ]
  //   })
  //   this.time()
  //   this.onClose()
  // },
  // 预定包厢
  toOrder(e) {
    const that = this

    if (wx.getStorageSync('mobile') == "") {
      that.login()
      return
    }
    let store_id = that.data.room[that.data.index].room_id.toString()
    let item = e.currentTarget.dataset.item
    console.log(22, store_id)
    wx.navigateTo({
      url: "/pages/index/roomselect/index?room_id=" + item.id + "&store_id=" + store_id,
    });
    // if (status == 1 || status == 3) {
    //   setTimeout(() => {
    //     Toast.fail("当前不可预约")
    //   }, 0)

    //   return
    // }
    // let status = e.currentTarget.dataset.status
    // let seat_id = e.currentTarget.dataset.id
    // 
    // let add_time = that.data.date_start + " " + that.data.time_start;
    // let end_time = that.data.date_end + " " + that.data.time_end;
    // // if (util.formatDate(add_time) < util.formatDate(new Date())) {
    // //   Toast.fail("请选择正确的预约时间")
    // //   return
    // // }
    // // console.log(util.formatDate(add_time), util.formatDate(new Date()))
    // app.http.post(app.api.makeOrder, {
    //   store_id,
    //   seat_id,
    //   add_time,
    //   end_time,
    //   package_id: that.data.package_id
    // }, (res) => {
    //   if (res.code == 1001) {
    //       Toast.fail(res.message)
    //   } else {
    //     wx.navigateTo({
    //       url: "/pages/index/order/index?store_id=" + store_id + "&order_on=" + res.data.order_on,
    //     });
    //   }
    // })

  },

  onShareTimeline() {}
});